package com.eis.persist.dao.util;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;

import com.eis.persist.model.function.PersistFunctionSchema;
import com.eis.thirdsys.connector.datatype.DataTypeList;
import com.eis.thirdsys.connector.datatype.datahandler.DataHandleUtil;
import com.eis.thirdsys.connector.datatype.eissystype.MysqlDataType;

public class FunctionTempDataBatchPreparedStatementSetter implements BatchPreparedStatementSetter {
	private static final Logger logger = LoggerFactory.getLogger(FunctionTempDataBatchPreparedStatementSetter.class);
	private ArrayList<HashMap<String, Object>> functionData;
	private List<PersistFunctionSchema> persistFunctionSchema;
	private DataTypeList dataTypes;
	
	public FunctionTempDataBatchPreparedStatementSetter(ArrayList<HashMap<String, Object>> functionData, 
			List<PersistFunctionSchema> persistFunctionSchema,
			DataTypeList dataTypes) {
		this.functionData = functionData;
		this.persistFunctionSchema = persistFunctionSchema;
		this.dataTypes = dataTypes;
	}
	
	@Override
	public void setValues(PreparedStatement ps, int i) throws SQLException {
		HashMap<String, Object> oneData = this.functionData.get(i);
		
		for(int j = 0; j < persistFunctionSchema.size(); j++) {
			PersistFunctionSchema oneField = persistFunctionSchema.get(j);
			Object handledData;
			try {
				handledData = DataHandleUtil.handleData(
						dataTypes.valueOf(oneField.getFieldType()).getHandlerClass(), 
						oneData.get(oneField.getFieldName()));
				
				if (handledData == null) handledData = "";
				
				if(dataTypes.valueOf(oneField.getFieldType()).getMysqlDataType().equals(MysqlDataType.VARCHAR)) {
					if (handledData instanceof String) {
						ps.setString(j + 1, (String) handledData);
					} else {
						ps.setString(j + 1, handledData.toString());
					}
					
				} else if(dataTypes.valueOf(oneField.getFieldType()).getMysqlDataType().equals(MysqlDataType.INT)) {
					if (handledData instanceof Integer) {
						ps.setInt(j + 1, (Integer) handledData);
					} else {
						String handledDataStr = handledData instanceof String ? (String) handledData : handledData.toString();
						try {
							ps.setInt(j + 1, Integer.parseInt(handledDataStr));
						} catch (Exception e) {
							ps.setInt(j + 1, 0);
						}
					}
				} else if(dataTypes.valueOf(oneField.getFieldType()).getMysqlDataType().equals(MysqlDataType.TINYINT)) {
					if (handledData instanceof Short) {
						ps.setShort(j + 1, (Short) handledData);
					} else {
						String handledDataStr = handledData instanceof String ? (String) handledData : handledData.toString();
						try {
							ps.setShort(j + 1, Short.parseShort(handledDataStr));
						} catch (Exception e) {
							ps.setShort(j + 1, (short) 0);
						}
					}
				} else if(dataTypes.valueOf(oneField.getFieldType()).getMysqlDataType().equals(MysqlDataType.SMALLINT)) {
					if (handledData instanceof Short) {
						ps.setShort(j + 1, (Short) handledData);
					} else {
						String handledDataStr = handledData instanceof String ? (String) handledData : handledData.toString();
						try {
							ps.setShort(j + 1, Short.parseShort(handledDataStr));
						} catch (Exception e) {
							ps.setShort(j + 1, (short) 0);
						}
					}
				} else if(dataTypes.valueOf(oneField.getFieldType()).getMysqlDataType().equals(MysqlDataType.MEDIUMBLOB)) {
					if (handledData instanceof String) {
						ps.setString(j + 1, (String) handledData);
					} else {
						ps.setString(j + 1, handledData.toString());
					}
				} else if(dataTypes.valueOf(oneField.getFieldType()).getMysqlDataType().equals(MysqlDataType.DOUBLE)) {
					if (handledData instanceof Double) {
						ps.setDouble(j + 1, (Double) handledData);
					} else {
						String handledDataStr = handledData instanceof String ? (String) handledData : handledData.toString();
						try {
							ps.setDouble(j + 1, Double.parseDouble(handledDataStr));
						} catch (Exception e) {
							ps.setDouble(j + 1, 0);
						}
					}
				} else if(dataTypes.valueOf(oneField.getFieldType()).getMysqlDataType().equals(MysqlDataType.MEDIUMINT)) {
					if (handledData instanceof Integer) {
						ps.setInt(j + 1, (Integer) handledData);
					} else {
						String handledDataStr = handledData instanceof String ? (String) handledData : handledData.toString();
						try {
							ps.setInt(j + 1, Integer.parseInt(handledDataStr));
						} catch (Exception e) {
							ps.setInt(j + 1, 0);
						}
					}
				} else if (dataTypes.valueOf(oneField.getFieldType()).getMysqlDataType().equals(MysqlDataType.TEXT)) {
					if (!(handledData instanceof String)) {
						handledData = handledData.toString();
					}
					ps.setString(j + 1, (String) handledData);
				} else if (dataTypes.valueOf(oneField.getFieldType()).getMysqlDataType().equals(MysqlDataType.DATETIME)) {
					if (handledData instanceof Date) {
						ps.setDate(j + 1, new java.sql.Date(((Date) handledData).getTime()));
					}
				}
			} catch (InstantiationException e) {
				logger.error("Exception: ", e);
			} catch (IllegalAccessException e) {
				logger.error("Exception: ", e);
			}
		}
	}

	@Override
	public int getBatchSize() {
		return functionData.size();
	}
}
